//
//  BADetailVC.m
//  YunFu
//
//  Created by Bager on 2018/9/5.
//  Copyright © 2018年 yunfu. All rights reserved.
//

#import "BADetailVC.h"

#import "BAFoodMenuVC.h"
#import "BACommentsVC.h"
#import "BARecommendListVC.h"
#import "CyclePagerView.h"

@interface BADetailVC ()<YNPageViewControllerDelegate,YNPageViewControllerDataSource,UIScrollViewDelegate>
{
    UIView *lineNav;
    UIView *changeView;
    BOOL isShow;
    YNPageViewController *pageView;
}

@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *type;
@property (nonatomic, strong) UIButton *addressBtn;
@property (nonatomic, strong) UILabel *price;
@property (nonatomic, strong) XHStarRateView *starView;
@property (nonatomic, strong) UIImageView *location;
@property (nonatomic, strong) UIButton *telBtn;

@end

@implementation BADetailVC

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self createView];
}

- (void)createNavView
{
    lineNav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:lineNav];
    
    changeView = [[UIView alloc] initWithFrame:lineNav.frame];
    changeView.backgroundColor = kPublicColorMain;
    changeView.alpha = 0;
    [self.view addSubview:changeView];

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithWhite:0 alpha:0.5].CGColor, (__bridge id)[UIColor clearColor].CGColor];
    gradientLayer.locations = @[@0.0, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1.0);
    gradientLayer.frame = CGRectMake(0, 0, kScreenWidth, kPublicNavHeight);
    [lineNav.layer addSublayer:gradientLayer];
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setFrame:CGRectMake(0, 20, 44, 44)];
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *btnMore = [[UIButton alloc] init];
    [btnMore setFrame:CGRectMake(kScreenWidth - 44 - 10, 20, 44, 44)];
    [btnMore setImage:[UIImage imageNamed:@"sandian"] forState:UIControlStateNormal];
    [btnMore addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnMore];
}

- (void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createView
{
    YNPageConfigration *configration = [YNPageConfigration defaultConfig];
    configration.pageStyle = YNPageStyleSuspensionTop;
    configration.headerViewCouldScale = YES;
    configration.showTabbar = NO;
    configration.suspenOffsetY = 64;
    configration.showNavigation = NO;
    configration.scrollMenu = NO;
    configration.aligmentModeCenter = NO;
    configration.lineWidthEqualFontWidth = YES;
    configration.showBottomLine = YES;
    configration.normalItemColor = [UIColor blackColor];
    configration.selectedItemColor = kPublicColorRed;
    configration.lineColor = kPublicColorRed;
    configration.bottomLineBgColor = [UIColor whiteColor];
    
    pageView = [YNPageViewController pageViewControllerWithControllers:[self getArrayVCs] titles:[self getArrayTitles] config:configration];
    pageView.delegate = self;
    pageView.dataSource = self;
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 270)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    CyclePagerView *page = [[CyclePagerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 180) imgArray:@[@"",@"",@""]];
    [headerView addSubview:page];
    
    DefineWeakSelf;
    page.callBack = ^(id result) {
        
    };
    
    [headerView addSubview:self.name];
    [headerView addSubview:self.price];
    [headerView addSubview:self.type];
    [headerView addSubview:self.starView];
    [headerView addSubview:self.location];
    [headerView addSubview:self.addressBtn];
    [headerView addSubview:self.telBtn];
    
    UIView *lineO = [[UIView alloc] initWithFrame:CGRectMake(kPublicMargin, 220, kScreenWidth, 1)];
    lineO.backgroundColor = kPublicColorLightGray;
    [headerView addSubview:lineO];
    
    UIView *lineT = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth - kPublicMargin - 50, 220 + 5, 0.5, 30)];
    lineT.backgroundColor = kPublicColorLightGray;
    [headerView addSubview:lineT];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 260, kScreenWidth, 8)];
    line.backgroundColor = kPublicColorLightGray;
    [headerView addSubview:line];
    
    pageView.headerView = headerView;
    
    [pageView addSelfToParentViewController:self];
    
    [self createNavView];
}

- (NSArray *)getArrayVCs
{
    BAFoodMenuVC *menu = [BAFoodMenuVC new];
    BACommentsVC *comments = [BACommentsVC new];
    BARecommendListVC *recommend = [BARecommendListVC new];

    return @[menu, comments, recommend];
}

- (NSArray *)getArrayTitles
{
    return @[@"堂食菜单", @"用户评价", @"更多推荐"];
}

- (UIScrollView *)pageViewController:(YNPageViewController *)pageViewController pageForIndex:(NSInteger)index
{
    if (index == 0)
    {
        BAFoodMenuVC *vc = pageViewController.controllersM[index];
        vc.callBack = ^(id result) {
            [self scrollChangeNavWithView:result];
        };
        return vc.collection;
    }
    else if (index == 1)
    {
        BACommentsVC *vc = pageViewController.controllersM[index];
        vc.callBack = ^(id result) {
            [self scrollChangeNavWithView:result];
        };
        return vc.table;
    }
    else
    {
        BARecommendListVC *vc = pageViewController.controllersM[index];
        vc.callBack = ^(id result) {
            [self scrollChangeNavWithView:result];
        };
        return vc.table;
    }
}

- (void)scrollChangeNavWithView:(UIScrollView *)view
{
    CGFloat offset_Y = view.contentOffset.y;

    NSLog(@"位置%lf", view.contentOffset.y);

    CGFloat minAlphaOffset_Y = -314;
    CGFloat maxAlphaOffset_Y = -110;
    CGFloat alpha = (offset_Y - minAlphaOffset_Y) / (maxAlphaOffset_Y - minAlphaOffset_Y);

    changeView.alpha = alpha;
    lineNav.alpha = 1 - alpha;
}

- (UILabel *)name
{
    if (!_name)
    {
        _name = [[UILabel alloc] initWithFrame:CGRectMake(kPublicMargin, 180, kScreenWidth - kPublicMargin - 120, 20)];
        _name.font = [UIFont systemFontOfSize:14];
        _name.adjustsFontSizeToFitWidth = YES;
        _name.text = @"艳遇锅包肉plus";
    }
    return _name;
}

- (UILabel *)price
{
    if (!_price)
    {
        _price = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 120 - kPublicMargin, 180, 120, 20)];
        _price.font = [UIFont systemFontOfSize:12];
        _price.textColor = [UIColor lightGrayColor];
        _price.adjustsFontSizeToFitWidth = YES;
        _price.textAlignment = NSTextAlignmentRight;
        _price.text = @"人均：¥66";
        _price.attributedText = [self convertColorString:_price.text];
    }
    return _price;
}

- (NSMutableAttributedString *)convertColorString:(NSString *)str
{
    NSMutableAttributedString *strnew = [[NSMutableAttributedString alloc]initWithString:str];
    [strnew addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, str.length - 3)];
    return strnew;
}

- (UILabel *)type
{
    if (!_type)
    {
        _type = [[UILabel alloc] initWithFrame:CGRectMake(kPublicMargin, CGRectGetMaxY(self.name.frame), 120, 20)];
        _type.font = [UIFont systemFontOfSize:12];
        _type.textColor = [UIColor grayColor];
        _type.text = @"烤鱼   南关区";
    }
    return _type;
}

- (XHStarRateView *)starView
{
    if (!_starView)
    {
        _starView = [[XHStarRateView alloc] initWithFrame:CGRectMake(kScreenWidth - kPublicMargin - 70, CGRectGetMaxY(self.name.frame), 70, 20)];
        _starView.currentScore = 4.3;
    }
    return _starView;
}

- (UIImageView *)location
{
    if (!_location)
    {
        _location = [[UIImageView alloc] initWithFrame:CGRectMake(kPublicMargin, 220, 30, 40)];
        _location.contentMode = UIViewContentModeCenter;
        [_location setImage:[UIImage imageNamed:@"yu1_map"]];
    }
    return _location;
}

- (UIButton *)addressBtn
{
    if (!_addressBtn)
    {
        _addressBtn = [[UIButton alloc] init];
        [_addressBtn setFrame:CGRectMake(kPublicMargin + 30, 220, kScreenWidth - kPublicMargin * 2 - 60 - 30, 40)];
        [_addressBtn setTitle:@"长春市朝阳区硅谷大街4073号长春市朝阳区硅谷大街4073号长春市朝阳区硅谷大街4073号" forState:UIControlStateNormal];
        _addressBtn.titleLabel.numberOfLines = 2;
        _addressBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        _addressBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
        [_addressBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        //    [btn addTarget:self action:@selector(addBankAccontClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addressBtn;
}

- (UIButton *)telBtn
{
    if (!_telBtn)
    {
        _telBtn = [[UIButton alloc] init];
        [_telBtn setFrame:CGRectMake(kScreenWidth - kPublicMargin - 50, 220, 50, 40)];
        [_telBtn setImage:[UIImage imageNamed:@"yu1_phone"] forState:UIControlStateNormal];
        //    [btn addTarget:self action:@selector(addBankAccontClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _telBtn;
}

@end
